package com.huiquan.vocab.controller;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.vocab.service.VocabDyadPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class VocabDyadPositionController {

	@Autowired
	private VocabDyadPositionService vocabDyadPositionService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/dyad_o_check_p_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("checkFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		// 获取页面参数，如果request中有值则修改session，否则从session中拿值
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String oKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "oKey");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");
		String orderByCnt = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "orderByCnt");

		return vocabDyadPositionService.list(start, oKey, exactSearchFlag, countPerPageStr,orderByCnt);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_o_check_p_init.do")
	public ReturnData init(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		return vocabDyadPositionService.init();
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_o_check_p_change_element_synonym.do")
	public ReturnData changeElementSynonym(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("id");
		String elementSynonym = req.getParameter("elementSynonym");

		return vocabDyadPositionService.changeElementSynonym(id, elementSynonym, user);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_o_check_p_submit_page.do")
	public ReturnData submitPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		User user = (User) req.getSession().getAttribute("user");
		String oStd = req.getParameter("ostd");
		if (oStd == null || oStd.isEmpty()) {
			return ReturnUtil.fail(ReturnCode.EXCEPTION);
		}

		return vocabDyadPositionService.submitPage(oStd, user);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_o_check_p_change_display_p.do")
	public ReturnData changeDisplayP(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		String id = req.getParameter("id");
		String displayP = req.getParameter("displayP");
		User user = (User) req.getSession().getAttribute("user");

		return vocabDyadPositionService.changeDisplayP(id, displayP,user);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_o_check_p_del_std.do")
	public ReturnData delStd(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		String id = req.getParameter("id");

		return vocabDyadPositionService.delStd(id);
	}
		
	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_o_check_p_submit_dyad.do")
	public ReturnData submitDyad(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		String o = req.getParameter("o");
		String p = req.getParameter("p");
		String position = req.getParameter("position");
		String elementSynonym = req.getParameter("elementSynonym");
		User user = (User) req.getSession().getAttribute("user");

		return vocabDyadPositionService.submitDyad(o, p, position, elementSynonym, user);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_o_check_p_change_display_flag.do")
	public ReturnData changeDisplayFlag(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		String id = req.getParameter("id");
		String displayFlag = req.getParameter("displayFlag");
		User user = (User) req.getSession().getAttribute("user");

		return vocabDyadPositionService.changeDisplayFlag(id, displayFlag, user);
	}

}
